package com.ruoyi.project.service.impl;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import com.ruoyi.common.domainenum.FinPackageStatus;
import com.ruoyi.common.domainenum.FinSubjectType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.common.StaticMethod;
import com.ruoyi.project.domain.FinSubjectMonth;
import com.ruoyi.project.mapper.FinSubjectMonthMapper;
import com.ruoyi.project.service.IBootstrapService;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.SecurityUtils;
import javax.annotation.Resource;
import com.ruoyi.project.mapper.FinPackageMapper;
import com.ruoyi.project.domain.FinPackage;
import com.ruoyi.project.service.IFinPackageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 账套Service业务层处理
 *
 * @author abel
 * @date 2024-10-12
 */
@Service
public class FinPackageServiceImpl implements IFinPackageService
{
    @Resource
    private FinPackageMapper finPackageMapper;

    @Resource
    private FinSubjectMonthMapper finSubjectMonthMapper;

    @Resource
    private IBootstrapService bootstrapService;
    /**
     * 查询账套
     *
     * @param id 账套ID
     * @return 账套
     */
    @Override
    public FinPackage selectFinPackageById(Long id)
    {
        return finPackageMapper.selectById(id);
    }

    /**
     * 获取账套，获取不到数据抛错
     *
     * @param id 账套ID
     * @return 账套
     */
    @Override
    public FinPackage getFinPackageById(Long id)
    {
        FinPackage finPackage = finPackageMapper.selectById(id);
        if(finPackage == null){
            throw new ServiceException("账套id不正确，没有找到相关数据！");
        }
        return finPackage;
    }

    /**
     * 查询账套列表
     *
     * @param finPackage 账套
     * @return 账套
     */
    @Override
    public List<FinPackage> selectFinPackageList(FinPackage finPackage)
    {
        return finPackageMapper.selectFinPackageList(finPackage);
    }

    /**
     * 新增账套
     *
     * @param finPackage 账套
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFinPackage(FinPackage finPackage) throws IOException {
        finPackage.setCreateId(SecurityUtils.getUserId());
        finPackage.setCreateBy(SecurityUtils.getUsername());
        finPackage.setCreateTime(DateUtils.getNowDate());
        finPackage.setUpdateId(SecurityUtils.getUserId());
        finPackage.setUpdateBy(SecurityUtils.getUsername());
        finPackage.setCreateTime(DateUtils.getNowDate());

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(finPackage.getStartTime());
        finPackage.setCurrentYear(calendar.get(Calendar.YEAR));
        finPackage.setCurrentMonth(calendar.get(Calendar.MONTH));
        finPackage.setUserId(SecurityUtils.getUserId());
        finPackage.setHasCheckVoucher(true);
        finPackage.setStatus(FinPackageStatus.ONLINE);
        finPackageMapper.insert(finPackage);
        bootstrapService.initData(finPackage);
        return 1;
    }

    /**
     * 修改账套
     *
     * @param finPackage 账套
     * @return 结果
     */
    @Override
    @Transactional
    public int updateFinPackage(FinPackage finPackage)
    {
        finPackage.setUpdateBy(SecurityUtils.getUsername());
        finPackage.setUpdateId(SecurityUtils.getUserId());
        finPackage.setUpdateTime(DateUtils.getNowDate());
        return finPackageMapper.updateById(finPackage);
    }

    /**
     * 批量删除账套
     *
     * @param ids 需要删除的账套ID
     * @return 结果
     */
    @Override
    public int deleteFinPackageByIds(Long[] ids)
    {
        for(Long id:ids){
            deleteFinPackageById(id);
        }
        return ids.length;
    }

    /**
     * 删除账套信息
     *
     * @param id 账套ID
     * @return 结果
     */
    @Override
    public int deleteFinPackageById(Long id)
    {
        return finPackageMapper.deleteFinPackageById(id);
    }

    /**
     * 结算损益
     * @param finPackage
     */
    @Override
    public void syEnd(FinPackage finPackage) {
        //TODO
        FinSubjectMonth queryMonth = new FinSubjectMonth();
        queryMonth.setYearValue(finPackage.getCurrentYear());
        queryMonth.setMonthValue(finPackage.getCurrentMonth());
        queryMonth.setSubjectType(FinSubjectType.SY);
        List<FinSubjectMonth> subjectMonthList =  finSubjectMonthMapper.selectFinSubjectMonthList(queryMonth);
    }
}
